<?php

namespace models\admin;

use libs\request as request;

class aktualnosci extends \models\_SkyCMSModel{
    
    /**
     * @route=(/aktualnosci)
     */
    public function index()
    {
        return array();
    }
    
    /**
     * @route=(/aktualnosci/dodaj)
     */
    public function dodaj()
    {
        
        $this->database->query()->insert('kategorie_gal',array(
                                                            'nazwa'=>request::post('tytul')
        ));
        
        
        $this->database->query()->insert('aktualnosci',array(
                                                            'tytul'=>request::post('tytul'),
                                                            'data'=>strtotime(request::post('data')),
                                                            'news_short'=>request::post('news_short'),
                                                            'news_long'=>request::post('news_long'),
                                                            'id_galerii'=>$this->db->insert_id
        ));
        
        
	$plikzdjecietmp = $_FILES['zdjecie']['tmp_name'];
	if (!empty($plikzdjecietmp))
        {
            $idtemp_banner = time();
            $plik_zdjecie_nazwa = $idtemp_banner.'.jpg';
            if(is_uploaded_file($plikzdjecietmp)) 
            {
                    move_uploaded_file($plikzdjecietmp, "images/aktualnosci/$plik_zdjecie_nazwa");
                    $this->miniatura_kwadrat("images/aktualnosci/", $plik_zdjecie_nazwa);

                    $zapytanie = "UPDATE aktualnosci SET zdjecie = '".$plik_zdjecie_nazwa."' WHERE id = '".$this->db->insert_id."'";                                   
                    $idzapytania = $this->db->query($zapytanie) or die ("Nie można dodać zdjecia (foto).");                     
            }
            else 
                echo "Nie udało się wgrać zdjęcia na serwer<br>";
	}
    }
        
    
    private function miniatura_kwadrat($path, $imagename) 
    {
        // $path - where image is located (image needs to be already on server!!)
        // $imagename - file name of image
        $site_max_thumbnails_size=200; // max size
        // load image and get image size
        $img = imagecreatefromjpeg( "{$path}{$imagename}" );
        $width = imagesx( $img );
        $height = imagesy( $img );

        // calculate thumbnail size
        if ($height>=$width) {
                $smallerSize=$width;
                $croppingStartHeight=($height-$smallerSize)/2;
                $croppingStartWidth=0;
        }

        else {
                $smallerSize=$height;
                $croppingStartHeight=0;
                $croppingStartWidth=($width-$smallerSize)/2;	
        }

        $croppedImage=imagecreatetruecolor($smallerSize, $smallerSize); // create square image
        $resizedImage=imagecreatetruecolor($site_max_thumbnails_size, $site_max_thumbnails_size);
        imagecopy($croppedImage, $img, 0, 0, $croppingStartWidth, $croppingStartHeight, $smallerSize, $smallerSize); // build cropped, square image
        imagecopyresampled ($resizedImage, $croppedImage, 0, 0, 0, 0, $site_max_thumbnails_size, $site_max_thumbnails_size, $smallerSize, $smallerSize); // copy and resize old image into new image
        $pathToThumbs=$path.'';
        $imagename2=explode('_',$imagename);

        if (!imagejpeg($resizedImage, "{$pathToThumbs}{$imagename2[0]}{$imagename2[1]}" )) // save thumbnail into a file
        $result='Nie można zapisać miniaturki w folderze docelowym!';

        ImageDestroy($croppedImage); 
        ImageDestroy($resizedImage);

        return $result;
    }
   
}

?>
